package com.chushouya.common.support;

import com.general.framework.core.enums.ContextUserType;
import com.general.framework.core.lang.Jsons;
import com.general.framework.core.lang.Lists;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.util.HashSet;
import java.util.Objects;


/**
 * 开发选项
 */
@Data
@Slf4j
public class DevOption {

    /**
     * 开发者模式
     * 开启后，使用buildDevUserContext中的用户信息，从而跳过登录
     */
    private static boolean devModel = false;

    private static Long userId;

    private static Long merchantId;

    private static Long storeId;

    private static Long clerkId;

    private static Long partnerId;

    private static ContextUserType contextUserType;

    /**
     * 构建Dev用户上下文
     */
    public static RequestUserContext buildDevUserContext() {
        // 经过网关的认证信息
        RequestUserContext userContext = new RequestUserContext();
        userContext.setUserId(userId);
        userContext.setStoreId(storeId);
        userContext.setMerchantId(merchantId);
        userContext.setPartnerId(partnerId);
        userContext.setClerkId(clerkId);
        userContext.setUserType(contextUserType.value());
        userContext.setUserName("devModel");
        log.info("===>当前为开发者模式,登录信息:{}", Jsons.toJsonString(userContext));
        userContext.setPermissions(new HashSet<>(Lists.asList("*:*:*")));
        userContext.setRoles(new HashSet<>(Lists.asList("admin")));
        return userContext;
    }



    public static void setDevModel(boolean devModel){
        DevOption.devModel = devModel;
        log.info("开发者模式:{}", DevOption.devModel);
    }

    public static void setDevModel(ContextUserType contextUserType, Long userId,Long clerkId, Long merchantId, Long storeId, Long partnerId){
        if (DevOption.devModel) {
            DevOption.userId = userId;
            DevOption.merchantId = merchantId;
            DevOption.storeId = storeId;
            DevOption.partnerId = partnerId;
            DevOption.clerkId = clerkId;
            DevOption.contextUserType = contextUserType;
            log.info("====================开发者模式已打开====================");
            log.info(">>> 当前角色:{}", DevOption.contextUserType.named());
            if (Objects.nonNull(DevOption.userId)) {
                log.info(">>> 用户ID:{}", DevOption.userId);
            }
            if (Objects.nonNull(DevOption.merchantId)) {
                log.info(">>> 回收商ID:{}", DevOption.merchantId);
            }
            if (Objects.nonNull(DevOption.storeId)) {
                log.info(">>> 门店ID:{}", DevOption.storeId);
            }
            if (Objects.nonNull(DevOption.partnerId)) {
                log.info(">>> 合作商ID:{}", DevOption.partnerId);
            }
            if (Objects.nonNull(DevOption.clerkId)) {
                log.info(">>> 业务员ID:{}", DevOption.clerkId);
            }
            log.info("=====================================================");
        }
    }

    public static boolean isDevModel() {
        return devModel;
    }

}
